
const app = getApp()
Page({
    data: {
        showPay: false,
        getValue: 0,
        awardMallId: 0,
        pay: 0,
        columns: ['余额支付(免密)', '微信支付'],
        mall: {},
        show: false, // 交易成功弹出层,
        overlay: false
    },
    onLoad: function (options) {
        this.setData({
            awardMallId: options.id
        })
        this.getMallDetail()
    },
    getMallDetail() {
        let _this = this
        app.util.request({
            url: 'api/mall/findById',
            method: 'get',
            data: {
                id: _this.data.awardMallId
            },
            success(response) {
                if (response.data.code == 0) {
                    _this.setData({
                        mall: response.data.data
                    })
                }
            }
        })
    },
    changeGetValue(e) {
        console.log(e)
        this.setData({
            getValue: e.detail.value
        })
    },
    openChoose() {
        this.setData({
            showPay: true
        })
    },
    closePop() {
        this.setData({
            showPay: false
        })
    },
    choosePay(e) {
        this.setData({
            pay: e.currentTarget.dataset.index,
            showPay: false
        })
    },
    onChange(e) {
        this.setData({
            pay: e.detail.index,
            showPay: false
        })
    },
    // 立即下单
    toWantGet() {
        let _this = this;
        if (_this.data.getValue == '') {
            wx.showToast({
                title: '请输入买入的金额',
                icon: 'none'
            })
            return
        } else {
            app.util.request({
                url: "api/deal/add",
                method: "post",
                data: {
                    price: _this.data.getValue,
                    awardMallId: _this.data.awardMallId,
                    pay: _this.data.pay,
                    state: 2
                },
                success(response) {
                    if (response.data.code == 200) {
                        _this.payorder(response.data.data)
                    } else if (response.data.code == 0) {
                        _this.setData({
                            show: true
                        })
                        // wx.showModal({
                        //     title: '提示',
                        //     content: '交易成功',
                        //     showCancel: false,
                        //     success (res) {
                        //         wx.navigateBack({
                        //             delta: 1
                        //           })
                        //     }
                        //     })
                    } else if (response.data.code == 402) {
                        wx.showToast({
                            title: '余额不足',
                            icon: 'none'
                        })
                    } else {
                        wx.showToast({
                            title: '交易失败,请稍后重试',
                            icon: 'none'
                        })
                    }
                }
            })
        }
    },
    onClose() {
        this.setData({
            show: false
        })
    },
    onKnow() {
        this.setData({
            show: false
        })
        wx.navigateBack({
            delta: 1
        })
    },
    payorder(outTradeNo) {
        let _this = this;
        _this.setData({
            loading: true
        })
        let data = {
            outTradeNo: outTradeNo
        };
        app.util.request({
            url: "wxPay/miniAppPay",
            data: data,
            method: "get",
            success(response) {
                let data = response.data.data;
                wx.requestPayment({
                    timeStamp: data.timeStamp,
                    nonceStr: data.nonceStr,
                    package: data.package,
                    signType: data.signType,
                    paySign: data.paySign,
                    appId: data.appId,
                    success(res) {
                        if (res.errMsg == "requestPayment:ok") {
                            wx.showToast({
                                title: '支付成功！',
                            })
                            _this.setData({
                                hasPay: true,
                                show: true
                            })
                            // setTimeout(() => {
                            //     wx.switchTab({
                            //         url: '/pages/market/market-index',
                            //     })
                            // }, 2000)
                        }
                    },
                    fail(res) {
                        console.log(res)
                        _this.setData({
                            hasPay: false
                        })
                    }
                })
            },
            complete() {
                _this.setData({
                    loading: false
                })
            }
        })
    }
})
